﻿<!DOCTYPE html>
<html lang="en">
<head>
    <title id="Description">Customized Aggregates in jqxDataTable</title>
    <meta name="description" content="This sample demonstrates how we can customize the aggregates rendering in jQWidgets DataTable.">
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdatatable.js"></script>
    <script type="text/javascript" src="../../scripts/demos.js"></script>
    <script type="text/javascript" src="../sampledata/generatedata.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // prepare the data
            var data = generatedata(200);

            var source =
            {
                localData: data,
                dataType: "array",
                datafields:
                [
                    { name: 'firstname', type: 'string' },
                    { name: 'lastname', type: 'string' },
                    { name: 'productname', type: 'string' },
                    { name: 'quantity', type: 'number' },
                    { name: 'price', type: 'number' }
                ],
                updateRow: function (rowid, rowdata, commit) {
                    // synchronize with the server - send update command   
                    commit(true);
                }
            };

            var dataAdapter = new $.jqx.dataAdapter(source);

            // initialize jqxDataTable
            $("#dataTable").jqxDataTable(
            {
                width: 850,
                source: dataAdapter,
                altrows: true,
                pageable: true,
                pagerButtonsCount: 10,
                aggregatesHeight: 25,
                showAggregates: true,
                columns: [
                  { text: 'First Name', columntype: 'textbox', datafield: 'firstname', width: 90 },
                  { text: 'Last Name', datafield: 'lastname', columntype: 'textbox', width: 90 },
                  { text: 'Product', datafield: 'productname', width: 170 },
                  { text: 'Quantity', datafield: 'quantity', width: 100, align: 'right', cellsAlign: 'right', cellsFormat: 'n2' },
                  {
                      text: 'Price', datafield: 'price', cellsAlign: 'right', align: 'right', cellsFormat: 'c2', aggregates: [{
                          'Total':
                            function (aggregatedValue, currentValue, column, record) {
                                var total = currentValue * parseInt(record['quantity']);
                                return aggregatedValue + total;
                            }
                      }],
                      aggregatesRenderer: function (aggregates, column, element) {    
                          var renderString = "<div style='margin: 4px; float: right;  height: 100%;'>";
                          renderString += "<strong>Total: </strong>" + aggregates.Total + "</div>";
                          return renderString;
                      }
                  }
                ]
            });
        });
    </script>
</head>
<body class='default'>
    <div id="dataTable"></div>
</body>
</html>
